package com.xkdx.caipiao.my;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.bank.AddBankInfo;
import com.xkdx.caipiao.presistence.bank.SupportBankListAction;
import com.xkdx.caipiao.presistence.bank.SupportBankListModule;
import com.xkdx.caipiao.presistence.bank.SupportBankListPresistener;
import com.xkdx.caipiao.presistence.bank.UpdateBankAction;
import com.xkdx.caipiao.presistence.bank.UpdateBankModule;
import com.xkdx.caipiao.presistence.bank.UpdateBankPresistener;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class UpdateBankActivity extends ThreadActivity implements
		OnClickListener {

	private ImageView back;
	private Spinner bank_spinner;

	private EditText bank_code;
	private TextView titleinfo;
	private Button commit;
	private SupportBankListAction action;
	private SupportBankListModule module;
	private SupportBankListPresistener presistener;
	private ArrayList<AddBankInfo> supportlist;

	private UpdateBankAction updateaction;
	private UpdateBankModule updatemodule;
	private UpdateBankPresistener updatepresistener;
	SharePrefenceUtil sp;
	String uid, token, bankid, banktype, bankcode;
	ArrayAdapter<String> adapter;
	String id = "";
	String cardId = "";
	String cardtype = "";
	String name = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_bank_caipiao);
		sp = new SharePrefenceUtil(UpdateBankActivity.this,
				SharePrefenceUtil.USERINFO);
		
		Bundle b = getIntent().getBundleExtra("edit");
		if (b != null && !(b.isEmpty())) {
			id = b.getString("id");
			cardId = b.getString("cardid");
			cardtype = b.getString("type");
			name = b.getString("name");

		}
		findView();
		startThread();
	}

	private void findView() {
		titleinfo = (TextView) findViewById(R.id.titleinfo);

		titleinfo.setText(getResources().getString(R.string.updatebank));

		bank_spinner = (Spinner) findViewById(R.id.spinner_bank);
		bank_code = (EditText) findViewById(R.id.ed_bank_code);
		String code = "".equals(cardId) ? "" : cardId;
		bank_code.setText(code);
		back = (ImageView) findViewById(R.id.back);
		commit = (Button) findViewById(R.id.btn_bank_commit);
		commit.setOnClickListener(this);
		back.setOnClickListener(this);
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (result.containsKey("0")) {
			supportlist = module.list;
			final String[] strid = new String[supportlist.size()];
			String[] strname = new String[supportlist.size()];
			for (int i = 0; i < supportlist.size(); i++) {
				strid[i] = supportlist.get(i).getBankId();
				strname[i] = supportlist.get(i).getBankName();
			}
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, strname);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			bank_spinner.setAdapter(adapter);
			// 添加下拉框选项选择**
			bank_spinner
					.setOnItemSelectedListener(new OnItemSelectedListener() {

						@Override
						public void onItemSelected(AdapterView<?> adapterView,
								View view, int position, long id) {
							banktype = strid[position];

						}

						@Override
						public void onNothingSelected(AdapterView<?> arg0) {

						}

					});

			

		} else {
			Toast.makeText(this, "支持的银行列表获取失败", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		action = new SupportBankListAction();
		module = new SupportBankListModule();
		presistener = new SupportBankListPresistener(UpdateBankActivity.this);
		presistener.setActitons(action);
		presistener.setModule(module);
		presistener.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.btn_bank_commit:
			uid = usrSP.getId();
			token = usrSP.getLogintoken();

			bankcode = bank_code.getText().toString();

			StartBindBank();
			break;
		}
	}

	public void showOnEditPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			if ("0000".equals(updatemodule.code)) {
				toast("成功绑定银行");
				finish();
			} else {
				toast(updatemodule.message);
			}

		} else {
			toast("绑定银行失败");
		}
	}

	private void StartBindBank() {
		updateaction = new UpdateBankAction(uid, token, id, banktype, bankcode);
		updatemodule = new UpdateBankModule();
		updatepresistener = new UpdateBankPresistener(UpdateBankActivity.this);
		updatepresistener.setActitons(updateaction);
		updatepresistener.setModule(updatemodule);
		updatepresistener.execute();
	}

	@Override
	public void onBackClick(View view) {

	}

}
